﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Web;
using YUCodeGen.Annotation;

namespace YUCodeGen.Utils
{
    public class PageUtil
    {
        public static string GetPartial(PropertyInfo pinfo)
        {
            string result = "_inputText";
            if (pinfo.PropertyType == typeof(Boolean))
            {
                result = "_inputBoolean";
            }
            
            return result;
        }
        public static string GetInputType(PropertyInfo pinfo)
        {
            string result = "text";
            if (pinfo.PropertyType == typeof(Boolean))
            {
                result = "checkbox";
            }
            else if (pinfo.PropertyType == typeof(DateTime))
            {
                result = "date";
            }
            else if (IsTel(pinfo))
            {
                result = "tel";
            }
            else if ( IsEmail(pinfo))
            {
                result = "email";
            }
            else if (IsPassword(pinfo))
            {
                result = "password";
            }
            else if (IsUrl(pinfo))
            {
                result = "url";
            }
            
            return result;
        }

        public static bool IsUrl(PropertyInfo pinfo)
        {
            return pinfo.CustomAttributes.Any(x => x.AttributeType == typeof(UrlAttribute));
        }

        public static bool IsTel(PropertyInfo pinfo)
        {
            return pinfo.CustomAttributes.Any(x => x.AttributeType == typeof(PhoneAttribute));
        }

        public static bool IsEmail(PropertyInfo pinfo)
        {
            return pinfo.CustomAttributes.Any(x => x.AttributeType == typeof(EmailAddressAttribute));
        }

        public static bool IsPassword(PropertyInfo pinfo)
        {
            return IsDataType(pinfo, DataType.Password);
        }

        public static bool IsCreate(PropertyInfo pinfo)
        {
            return pinfo.CustomAttributes.Any(x => x.AttributeType == typeof(Create));
        }

        public static bool IsListFiled(PropertyInfo pinfo)
        {
            return pinfo.CustomAttributes.Any(x => x.AttributeType == typeof(ListField));
        }

        public static string GetMaxlength(PropertyInfo pinfo)
        {
            string result = "";
            var maxlengthAttri= pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(MaxLengthAttribute));
            if (maxlengthAttri != null)
            {
                result = $"maxlength=\"{maxlengthAttri.ConstructorArguments[0].Value}\"";
            }
            return result;
        }

        public static bool HasForbiddenName(PropertyInfo pinfo)
        {
            return false;
        }

        public static bool HasRegularExp(PropertyInfo pinfo)
        {
            return !string.IsNullOrEmpty(GetRegularExp(pinfo));
        }

        public static string GetMinLengthValue(PropertyInfo pinfo)
        {
            string result = "";
            var attri = pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(MaxLengthAttribute));
            if (attri != null)
            {
                result = attri.ConstructorArguments[0].Value.ToString();
            }
            return result;
        }

        

        public static string GetRequired(PropertyInfo pinfo)
        {
            string result = "";
            var attri = pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(RequiredAttribute));
            if (attri != null)
            {
                result = "required";
            }
            return result;
        }

        public static string GetRegularExp(PropertyInfo pinfo)
        {
            string result = "";
            var attri = pinfo.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(RegularExpressionAttribute));
            if (attri!=null)
            {
                result =$"pattern=\"{attri.ConstructorArguments[0].Value}\"";
            }
            return result;
        }

        public static bool IsMultipleText(PropertyInfo pinfo)
        {
            return IsDataType(pinfo, DataType.MultilineText);
            //bool result = false;
            //var attri = pinfo.CustomAttributes.Where(x => x.AttributeType == typeof(DataTypeAttribute));
            //if (attri.Count()>0)
            //{
            //    result = (attri.FirstOrDefault(x =>(int)(x.ConstructorArguments[0].Value) ==(int)DataType.MultilineText) != null);
            //}
            //return result;
        }

        public static bool IsDataType(PropertyInfo pinfo, DataType typeEnum)
        {
            bool result = false;
            var attri = pinfo.CustomAttributes.Where(x => x.AttributeType == typeof(DataTypeAttribute));
            if (attri.Count() > 0)
            {
                result = (attri.FirstOrDefault(x => (int)(x.ConstructorArguments[0].Value) == (int)typeEnum) != null);
            }
            return result;
        }

        public static string Capitalize(string val)
        {

            string result = val;
            if (result.Length > 0)
            {
                result = $"{result.Substring(0, 1).ToUpper()}{val.Substring(1, val.Length - 1)}";
            }
            return val;
        }


    }
}